package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.constants.DictConstants;
import com.yu.crm.domain.pojo.WfActionInfo;
import com.yu.crm.domain.pojo.WfCombiCondition;
import com.yu.crm.domain.pojo.WfCombiInfo;
import com.yu.crm.domain.request.ConditionInfoConfReq;
import com.yu.crm.domain.request.ConditionInfoReq;
import com.yu.crm.domain.request.ConditionInfoUpdateReq;
import com.yu.crm.domain.response.ActionRep;
import com.yu.crm.domain.response.ConditionInfoRep;
import com.yu.crm.domain.response.ConditionTypeRep;
import com.yu.crm.domain.response.DetailedConditions;
import com.yu.crm.mapper.WfCombiInfoMapper;
import com.yu.crm.service.WfActionInfoService;
import com.yu.crm.service.WfCombiConditionService;
import com.yu.crm.service.WfCombiInfoService;
import com.yu.crm.utils.ConditionUtil;
import com.yxzz.common.exception.GeneralException;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.DateHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 流程条件组合 服务实现类
 * </p>
 *
 * @author donnie-bin
 * @since 2019-10-08
 */
@Service
public class WfCombiInfoServiceImpl extends ServiceImpl<WfCombiInfoMapper, WfCombiInfo> implements WfCombiInfoService {

    @Autowired
    private WfActionInfoService actionInfoService;
    @Autowired
    private WfCombiConditionService wfCombiConditionService;

    /**
     * 条件判断确定下一个状态
     */
    @Override
    public ActionRep conditionalJudgment(ConditionInfoReq conditionInfoReq) {
        ActionRep actionRep = new ActionRep();
        Map<String, Integer> map = conditionInfoReq.getMap();
        QueryWrapper<WfActionInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id",conditionInfoReq.getAppId());
        queryWrapper.eq("action_enum_id",conditionInfoReq.getActionEnumId());
        queryWrapper.eq("p_node_id",conditionInfoReq.getNodeId());
        List<WfActionInfo> wfActionInfoList = actionInfoService.list(queryWrapper);
        if (DataHelpers.isEmpty(wfActionInfoList) ) {
            throw new GeneralException("该条动作记录不存在");
        }
        for (WfActionInfo wfActionInfo:wfActionInfoList
             ) {
            boolean b = true;
            if (DataHelpers.isNotEmpty(wfActionInfo.getCombiId())) {
                ConditionInfoRep conditionInfoDetail = getConditionInfoDetail(conditionInfoReq.getAppId(), wfActionInfo);
                b = ConditionUtil.ConditionMap(map, conditionInfoDetail);
            }
            if (b) {
                //条件满足返回下一个节点
                Integer status = actionInfoService.getStatus(wfActionInfo.getCombiId(),conditionInfoReq.getNodeId(), conditionInfoReq.getActionEnumId());
                actionRep.setStatusId(status);
                actionRep.setNodeId(wfActionInfo.getNodeId());
                return actionRep;
            }else {
                //条件不满足返回当前节点
                actionRep.setNodeId(conditionInfoReq.getNodeId());
                actionRep.setStatusId(0);
            }
        }

        return actionRep;
    }

    /**
     * 条件类型集合
     */
    @Override
    public List<ConditionTypeRep> getConditionTypeList() {
        return baseMapper.getConditionTypeList(DictConstants.CONDITION_TYPE);
    }

    /**
     * 只考虑一层条件结构
     * 通过应用id和父节点id查询条件明细
     */
    public ConditionInfoRep getConditionInfoDetail(String appId, WfActionInfo wfActionInfo) {
        ConditionInfoRep conditionInfoRep = new ConditionInfoRep();
        WfCombiInfo wfCombiInfo = baseMapper.selectById(wfActionInfo.getCombiId());
        QueryWrapper query = new QueryWrapper();
        query.eq("wce.app_id",appId);
        query.eq("wcc.is_deleted",0);
        query.eq("wce.is_deleted",0);
        if (DataHelpers.isNotEmpty(wfCombiInfo)) {
            query.eq("wcc.combi_id",wfCombiInfo.getId());
            conditionInfoRep.setCombiType(wfCombiInfo.getCombiName());
        }
        List<DetailedConditions> list = baseMapper.getConditionDetail(query);
        conditionInfoRep.setDetailedConditionsList(list);
        return conditionInfoRep;
    }

    /**
     * 通过appId做删除
     * @param appId
     */
    public void del(String appId) {
        QueryWrapper<WfCombiInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id",appId);
        baseMapper.delete(queryWrapper);
    }


    /**
     * 修改条件信息
     */
    @Override
    public void conditionUpdate(ConditionInfoUpdateReq conditionInfoUpdateReq) {
        String appId = conditionInfoUpdateReq.getAppId();
        //条件信息表修改
        WfCombiInfo wfCombiInfo = baseMapper.selectById(conditionInfoUpdateReq.getCombiInfoId());
        if (DataHelpers.isEmpty(wfCombiInfo)) {
            throw new GeneralException("该条件信息不存在");
        }
        wfCombiInfo.setCombiName(conditionInfoUpdateReq.getConditionRelationValue());
        wfCombiInfo.setGmtModified(DateHelpers.now());
        baseMapper.updateById(wfCombiInfo);

        //条件组合表
        //先删除后新增
        wfCombiConditionService.del(appId);
        List<ConditionInfoConfReq> conditionInfoConfReqList = conditionInfoUpdateReq.getConditionInfoConfReqList();
        if (DataHelpers.isNotEmpty(conditionInfoConfReqList) && conditionInfoConfReqList.size()>0) {
            List<WfCombiCondition> wfCombiConditions = new ArrayList<>();
            for (ConditionInfoConfReq confReq:conditionInfoConfReqList
            ) {
                WfCombiCondition wfCombiCondition = new WfCombiCondition();
                wfCombiCondition.setCombiId(wfCombiInfo.getId());
                wfCombiCondition.setConditionId(confReq.getConditionEnumId());
                wfCombiCondition.setConditionSymbol(confReq.getConditionType());
                wfCombiCondition.setConditionValue(confReq.getConditionValue());
                wfCombiCondition.setGmtCreate(DateHelpers.now());
                wfCombiCondition.setAppId(appId);
                wfCombiCondition.setIsDeleted(0);
                wfCombiConditions.add(wfCombiCondition);
            }
            if (wfCombiConditions.size()>0) {
                wfCombiConditionService.saveBatch(wfCombiConditions);
            }
        }



    }

}
